const {ccclass, property} = cc._decorator;

@ccclass
export default class sound extends cc.Component {
    static base_sound_url:string = 'sounds/';
    
    static effect_flag:boolean = true;
    /**
     * 播放音乐
     * @param name: 音效名字
     * @param loop: 是否循环
     */    
    public static play_music(name:string,loop:boolean)
    {
        if(orc.orc_data.local_data.voice_switch)
        {
            cc.audioEngine.stopMusic();
            cc.resources.load(this.base_sound_url+name,cc.AudioClip,(err, audioClip:cc.AudioClip) => {                
                cc.audioEngine.playMusic(audioClip,loop)
            });
        }
    }

     /**
     * 停止音乐    
     */
    public static stop_music()
    {
        cc.audioEngine.stopMusic();
    }

    /**
     * 播放音效
     * @param name: 音效名字
     * @param wait: 是否等待,等待上一个音效播放完
     * @param volume: 音量大小
    */
    public static play_effect(name:string,wait:boolean = true,volume:number = 1)
    {    
        if(orc.orc_data.local_data.voice_switch)
        {            
            if(this.effect_flag || !wait)
            {
                cc.resources.load(this.base_sound_url+name,cc.AudioClip,(err, audioClip:cc.AudioClip) => { 
                    this.effect_flag = false;
                    
                    let id = cc.audioEngine.playEffect(audioClip,false);
                    cc.audioEngine.setVolume(id,volume);
                    this.effect_flag = false;
                    cc.audioEngine.setFinishCallback(id,()=>{
                        this.effect_flag = true;                        
                    })
                });                    
            }
        } 
    }
}
